<?php
/**
 * 购买产生积分脚本生成
 * 也是7天后给,跟返点一样.
 */
require_once(dirname(__FILE__)."/../config/include.php");
	
$GLOBALS['DEBUG_SQL_LOG'] = false ;


sql_connect() ;


function log_save_score($logs)
{
	$time = date("Ymd H:i:s") ;
	$save_log_filename = dirname(__FILE__)."/log_core_pay_score.txt" ;
	file_put_contents($save_log_filename,"$time -- $logs .\r\n",FILE_APPEND);
}


$start = 0 ;
$limit = 50 ;

log_save_score("开始搜索及计算用户产生的积分:") ;


$score_bl = intval(sql_fetch_one_cell("select `config_value` from  `prf_config` where `config_attribute` = 'buy_refund_point' and `description` = 1")) ;

while(true){
	
	$paylist = sql_fetch_rows("select * from pay_trade where `trade_state` = 3 and unix_timestamp() - `over_time` >= 7 * 24 * 60 * 60 and `trade_no` not in (select `score_key` from `log_user_score` where `state` = 1) limit $start , $limit") ;
	if (is_array($paylist)) foreach($paylist as &$u){
		$pay_uid = $u['user_id'] ;		// 付款用户
		$trade_no = $u['trade_no'] ;	// 订单号
		$all_money = intval($u['price_modify']) ;	// 用户付款的金额
		
		
		// 运营商（用户类型为5）和装饰公司（用户类型为4）和协会（用户类型为6）和服务商（用户类型为7）的用户购买没有积分。
		$user_info = sql_fetch_one("select * from user_info where `id` = '$pay_uid'") ;
		$user_state = intval($user_info['user_type']) ;					// 用户类型
		
		$score = 0 ;
		$memo =  "购买花费 $all_money , 积分生成比例 $score_bl : 1" ;
		
		if ($user_state == 4 || $user_state == 5 || $user_state == 6 || $user_state == 7){
			$score = 0 ;
			$memo = "此用户类型 user_type = ". $user_state . " , 不产生积分" ;
		}else{
			$score = intval($all_money / $score_bl) ;
		}
        addUserScore($pay_uid, $score, 1 ,$memo, $trade_no);

		log_save_score("$pay_uid 购买产生积分 : " . $memo) ;		
	}else{
		break ;
	}
	if (count($paylist) != $limit){
		break ;
	}	
}

?>